package customer.zspt.cn.customer.utils;
import android.media.AudioManager;
import android.media.MediaPlayer;

import java.io.IOException;

import customer.zspt.cn.customer.Widget.ChatVocieLeftMsgItem;
import customer.zspt.cn.customer.Widget.chat.ChatVocieMsgItem;

/**
 * Created by Administrator on 2018/8/8 0008.
 */
public class MediaPlayUtils {
      public  MediaPlayer mediaPlayer;
      public static MediaPlayUtils mediaPlayUtils = null;
      public  void stop(){
          setStartAm(false);
          if(mediaPlayer!=null){
              mediaPlayer.release();
              mediaPlayer = null;
          }
      }
      /**单利模式*/
       public static MediaPlayUtils getInstance(){
           if(mediaPlayUtils==null){
               mediaPlayUtils = new MediaPlayUtils();
           }
           return mediaPlayUtils;
       }
      /**播放网络音频*/
      public void playUrl(String url){
              stop();
              mediaPlayer = new MediaPlayer();
              mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
           try {
                  mediaPlayer.setDataSource(url);
                  mediaPlayer.prepareAsync();
                  mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                      @Override
                      public void onPrepared(MediaPlayer mp) {
                          mediaPlayer.start();
                          setStartAm(true);
                      }
                  });
                  mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                      @Override
                      public void onCompletion(MediaPlayer mp) {
                          setStartAm(false);
                      }
                  });
              } catch (IOException e) {
                  e.printStackTrace();
              }
      }
    /**播放本地音频*/
    public  void playLoad(String url){
        stop();
        mediaPlayer = new MediaPlayer();
        mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        try {
            mediaPlayer.setDataSource(url);
            mediaPlayer.prepare();
            mediaPlayer.start();
            setStartAm(true);
            mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
                    setStartAm(false);
                }
            });
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
      public  Object object;
    /**设置点击播放的对象*/
      public  void setPreparedListener(Object object){
          if(object!=null){
              setStartAm(false);
          }
          this.object = object;
      }
      public void setStartAm(boolean isPlay){
          if(object!=null){
              if(object instanceof ChatVocieLeftMsgItem){
                  ChatVocieLeftMsgItem chatVocieLeftMsgItem = (ChatVocieLeftMsgItem) this.object;
                  chatVocieLeftMsgItem.IsPlayAnimation(isPlay);
                  chatVocieLeftMsgItem.IsPlay = isPlay;
              }else if(object instanceof ChatVocieMsgItem){
                  ChatVocieMsgItem chatVocieRightMsgItem = (ChatVocieMsgItem) this.object;
                  chatVocieRightMsgItem.IsPlayAnimation(isPlay);
                  chatVocieRightMsgItem.IsPlay = isPlay;
              }
          }
      }
}
